<template>

    <layout>
        <mt-header fixed title="问答详情">
            <a href="javascript:window.history.go(-1);" slot="left">
                <mt-button icon="back"></mt-button>
            </a>
        </mt-header>
        <div class="top_title">
            <div class="top_img">
                <img :src="showImage(post.headImgUrl, '?x-oss-process=image/resize,h_100')"/>
            </div>
            <div class="top_name">
                <div class="name">{{ post.name }}</div>
                <div class="time">{{ post.createDate }}</div>
            </div>
        </div>
        <div class="txte_box">{{ post.title }}</div>
        <div class="text_title">
            <template v-if="post.content != '' && post.content != undefined">
                <p v-html="post.content"></p>
            </template>
            <template v-else>
                <template v-for="item in post.jsoups">
                    <p v-if="item.content != ''">
                        {{item.content}}
                    </p>
                    <template v-if="item.image != '' && item.image != undefined">
                        <img width="100%" :src="showImage(item.image)"/>
                    </template>
                </template>
            </template>
        </div>
        <div class="top_ft">
            <div class="top_l">26回答 3620浏览</div>
            <div class="top_r">2040收藏</div>
        </div>
        <div class="page-infinite-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }">
            <ul class="ui_list page-infinite-list" v-infinite-scroll="loadMore" infinite-scroll-disabled="loading" infinite-scroll-distance="50">
                <li class="item" v-for="item in list">
                    <div class="list_img">
                        <div class="list_l">
                            <img :src="showImage(item.headImgUrl, '?x-oss-process=image/resize,h_100')"/>
                        </div>
                        <div class="list_r">{{ item.name}}</div>
                    </div>
                    <div class="list_c">{{ item.title}}！！！！</div>
                    <div class="list_f">
                        <div class="list_f_l">{{ item.postLikeCount }}赞 &nbsp; {{ item.commentCount }}回答</div>
                        <div class="list_f_r"> {{ item.createDate }}</div>
                    </div>
                </li>
            </ul>
            <p v-show="loading" class="page-infinite-loading">
                <mt-spinner :size="20" type="fading-circle"></mt-spinner>
                <span>加载中...</span>
            </p>
        </div>
        <div class="footer">


            <div v-if="post.collectionId" class="collection icon-yishoucang" @click="collect">收藏</div>
            <div v-show="isNone" v-else="post.collectionId" class="collection icon-shoucang"  @click="collect">收藏</div>
            <div v-show="isShow" v-if="!post.collectionId" class="collection icon-yishoucang"  >收藏</div>




            <div class="icon-chat" @click="commentStatus = true">我来回答</div>
        </div>
        <div class="footers" v-show="commentStatus">
            <span>
                <input type="text" v-model="content" placeholder="开始你的评价">
                <span class="sumint" @click="comment" v-if="content != ''">发送</span>
                <span class="sumint cancle" @click="commentStatus = false" v-else>取消</span>
            </span>
        </div>

    </layout>
</template>
<style lang="scss">
    @import "index";
</style>
<script type="text/babel">
    import { api, image } from 'framework/config.js';
    import Layout from "component/layout/standard";

    import Vue from 'vue'
    import Mint from 'mint-ui';
    Vue.use(Mint);

    export default {
        components: {
            Layout
        },
        data(){
            return {
                selected: '1',
                list: [],
                lastPage: false,
                pageNo: 0,
                nextPage: 1,
                sort: 0,
                type: '',
                loading: false,
                wrapperHeight: 0,
                commentStatus: false,
                content: '',
                isShow:false,
                isNone:true,
            }
        },
        computed: {
            api(){
                return api();
            }
        },
        methods: {
            showImage(url, params){
                return image(this.api.image, url, params);
            },
            loadMore(){
                if (this.lastPage){
                    return;
                }

                this.loading = true;
                this.$http.get('/api/question/list', {
                    params: {
                        pageNo: this.nextPage
                    }
                }).then((response) => {
                    if (response.status == 200){
                        let data = response.data;
                        if (data.code == 200) {
                            data = data.data;
                            this.lastPage = data.lastPage;
                            this.pageNo = data.pageNo;
                            this.nextPage = data.nextPage;

                            let list = data.list;
                            for (let i = 0; i < list.length; i++){
                                this.list.push(list[i]);
                            }

                        }

                    }else{
                        this.lastPage = true;
                    }
                    this.loading = false;
                })
            },
            collect(){
                let param = new URLSearchParams();
                param.append("type", 3);
                param.append("id", this.post.id);
                this.$http.post('/api/collect/add',
                    param
                ).then((response) => {
                    if (response.status == 200){
                        let data = response.data;
                        if (data.code == 200) {
                            Mint.Toast('收藏成功');
                            this.isShow=true
                            this.isNone=false

                        } else {
                            Mint.Toast(data.message);
                        }
                    }else{
                    }
                })
            },
            comment(){
                let param = new URLSearchParams();
                param.append("type", 1);
                param.append("id", this.post.id);
                param.append("content", this.content);

                this.$http.post('/api/comment/post/add',
                    param
                ).then((response) => {
                    if (response.status == 200){
                        let data = response.data;
                        if (data.code == 200) {
                            Mint.Toast('评论成功');
                            this.commentStatus = false;
                            this.content = '';

                            this.reload(function () {
                                var height = document.querySelector('#commentContainer').offsetTop + window.screen.height;
                                window.scrollTo({"behavior": "smooth", "top": height });
                            });
                        }
                    }else{
                    }
                })
            }
        },
        created(){
        },
        mounted() {
            this.wrapperHeight = document.documentElement.clientHeight - this.$refs.wrapper.getBoundingClientRect().top;
        }
    }
</script>

